package com.rdc.qingkou.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.contract.ChangePasswordContract;
import com.rdc.qingkou.entity.ModifyUserInfoResult;
import com.rdc.qingkou.entity.Result;
import com.rdc.qingkou.presenter.ChangePasswordPresenter;
import com.rdc.qingkou.utils.MyUtils;


public class ChangePasswordActivity extends AppCompatActivity implements ChangePasswordContract.IView{

    private EditText et_oldPassword;
    private EditText et_newPassword;
    private EditText et_confirmPassword;

    private Activity context;

    //private String oldPassword;

    private ChangePasswordPresenter changePasswordPresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.activity_change_password);

        context = this;

        changePasswordPresenter = new ChangePasswordPresenter(this);
        initView();
//        changePasswordPresenter.showPassword(MyApplication.getUser().getUserId());  //ohDrat

    }

    private void initView(){
        et_oldPassword=(EditText)findViewById(R.id.et_oldPassword);
        et_newPassword=(EditText)findViewById(R.id.et_newPassword);
        et_confirmPassword=(EditText)findViewById(R.id.et_confirmPassword);

        findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (et_newPassword.getText().length()<6){
                    Toast.makeText(getApplicationContext(),"密码不能少于6位！",Toast.LENGTH_SHORT).show();
                }else if (!et_newPassword.getText().toString().equals(et_confirmPassword.getText().toString())){
                    Toast.makeText(getApplicationContext(),"新密码与确认密码不一致！",Toast.LENGTH_SHORT).show();
                }else {
                    changePasswordPresenter.changePassword(String.valueOf(MyApplication.getUser().getUserId()),
                            et_oldPassword.getText().toString(),
                            et_newPassword.getText().toString(),
                            et_confirmPassword.getText().toString());
                }

                MyUtils.hideKeyboard(context);
            }
        });
        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void getPassword(ModifyUserInfoResult modifyUserInfoResult) {

    }

    @Override
    public void showModifyResult(Result result) {
        Log.d("ChangePasswordActivity","showModifyResult is : " +  result.getResult());
        if (result.getResult().equals("fail")){
            Toast.makeText(this,"修改错误！原因 ：" + result.getReason(),Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"修改成功！",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showInfo(String info) {
        Log.d("ChangePasswordActivity","result is : " + info);
    }
}
